package cn.hjc.service;

import cn.hjc.dao.DeptDao;
import cn.hjc.dao.HibernateUtil;
import cn.hjc.entity.Dept;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.List;

public class DeptBiz  {
    private DeptDao deptDao = new DeptDao();

    public Dept findDeptById(Byte id){
        Transaction tx = null;
        Dept result = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();//开启事务

         //   result = deptDao.get(id);//调用Dao方法，根据OID加载指定Dept对象
            //输出结果，与调用get()方法时不同
            //原因会在后续章节中有关延迟加载的内容中进行分析
            result = deptDao.load(id);
            System.out.println(result.getDeptName());
            tx.commit();//提交事务

        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback(); //回滚事务
        }
        return  result;
    }

    public  void addNewDept(Dept dept){
        Transaction tx = null;
        try {
            tx = deptDao.currentSession().beginTransaction();//开启事务
            deptDao.save(dept);//提交事务
            tx.commit();//提交事务
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null){
                tx.rollback();//回滚事务
            }
        }
    }


    public void updateDept(Dept dept){
        Transaction tx = null;
        try {
            tx = deptDao.currentSession().beginTransaction();//开启事务
            //通过get()或load()方法加载要修改的部门对象
            Dept deptToUpdate = deptDao.load(dept.getDeptNo());
            //更新部门数据
            deptToUpdate.setDeptName(dept.getDeptName());
            deptToUpdate.setLocation(dept.getLocation());
            tx.commit();//提交事务

        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback();  //回滚事务
        }
    }

    public  void deleteDept(Dept dept){
        Transaction tx = null;
        try {
            tx = deptDao.currentSession().beginTransaction();   //开启事务


            deptDao.delete(dept);   //删除部门数据
            tx.commit();//提交事务
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback();//回滚事务
        }
    }

        public Dept mergeDept(Dept dept){
        Transaction tx = null;
        Dept persistentDept = null;
        try {
            tx = deptDao.currentSession().beginTransaction();//开启事务
            //合并dept的数据或包车dept的副本，返回持久态对象
            persistentDept = deptDao.merge(dept);
            tx.commit();
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback();
            }
        return persistentDept;
        }

            //ch05示例3
    public List<Dept> findDeptByName(String deptName){
        Transaction tx  = null;
        List<Dept> result = null;
        try {
            tx = deptDao.currentSession().beginTransaction();//开启事务
            result = deptDao.findByDeptName(deptName);//调用dao的查询方法
            tx.commit();//提交事务
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback();//回滚事务

        }
        return  result;
    }
}
